package com.xbq.exceleditor.adapter

import android.graphics.Paint
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xbq.exceleditor.R
import com.xbq.exceleditor.bean.model.ProductItem
import com.xbq.exceleditor.databinding.ItemProductBinding
import com.xbq.xbqsdk.net.common.vo.ProductVO
import javax.inject.Inject

/**
 * Author: liaohaiping
 * Time: 2020-11-05
 * Description:
 */
class ProductAdapter @Inject constructor(): BaseQuickAdapter<ProductItem, BaseViewHolder>(R.layout.item_product) {

    fun selectedProduct():ProductVO?{
        return data.firstOrNull { it.checked}?.product
    }

    override fun convert(holder: BaseViewHolder, item: ProductItem) {
        ItemProductBinding.bind(holder.itemView).let {
            it.tvOldPrice.paint.flags= Paint.STRIKE_THRU_TEXT_FLAG
            it.tvPrice.text = item.price()
            it.tvOldPrice.text = item.oldPrice()
            it.tvProductName.text = item.product.name
            it.tvDescription.text = item.product.description
            it.imgXscx.isVisible = item.index==0
            if(item.checked){
                it.tvDescription.setBackgroundResource(R.drawable.bg_buy_tag_on)
                it.itemContainer.setBackgroundResource(R.drawable.icon_buy_on)
                val textColor = context.resources.getColor(R.color.buy_vip_bg)
                it.tvPrice.setTextColor(textColor)
                it.tvOldPrice.setTextColor(textColor)
                it.tvProductName.setTextColor(textColor)
            }else{
                it.tvDescription.setBackgroundResource(R.drawable.bg_buy_tag_def)
                it.itemContainer.setBackgroundResource(R.drawable.icon_buy_def)
                val textColor = context.resources.getColor(R.color.vip_color)
                it.tvPrice.setTextColor(textColor)
                it.tvOldPrice.setTextColor(textColor)
                it.tvProductName.setTextColor(textColor)
            }
        }

    }
}